package org.glut.cloud.userManage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.glut.cloud.userManage.entity.RepairPersonnel;
import org.glut.cloud.userManage.service.IRepairPersonnelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 维修人员表 前端控制器
 *
 * @author Venus
 * @since 2024-08-13 11:45
 */
@RestController
@RequestMapping("/repair")
@CrossOrigin(origins = "http://localhost:5173") // 添加跨域注解
public class RepairPersonnelController {

    @Autowired
    private IRepairPersonnelService repairPersonnelService;

    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody Map<String, String> loginRequest) {
        String username = loginRequest.get("username");
        String password = loginRequest.get("password");

        RepairPersonnel repairPersonnel = repairPersonnelService.getOne(
                new LambdaQueryWrapper<RepairPersonnel>()
                        .eq(RepairPersonnel::getUsername, username)
                        .eq(RepairPersonnel::getPassword, password)
        );
        Map<String, Object> response = new HashMap<>();
        if (repairPersonnel != null) {
            response.put("success", true);
            response.put("message", "登录成功");
            response.put("repairPersonnel", repairPersonnel);
        } else {
            response.put("success", false);
            response.put("message", "用户名或密码错误");
        }
        return response;
    }

    @GetMapping("/me")
    public Map<String, Object> getUserInfo(@RequestParam Integer repairPersonnelId) {
        RepairPersonnel repairPersonnel = repairPersonnelService.getById(repairPersonnelId);
        Map<String, Object> response = new HashMap<>();
        if (repairPersonnel != null) {
            response.put("success", true);
            response.put("repairPersonnel", repairPersonnel);
        } else {
            response.put("success", false);
            response.put("message", "用户信息获取失败");
        }
        return response;
    }
}
